package course.dmsSpringBoot.service.Impl;

import course.dmsSpringBoot.entity.Administrator;
import course.dmsSpringBoot.mapper.AdministratorMapper;
import course.dmsSpringBoot.service.AdministratorService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author zouran
 * createDate：2023/12/18  21:31
 */
@Service
public class AdministratorServiceImpl implements AdministratorService {
    private final AdministratorMapper administratorMapper;

    public AdministratorServiceImpl(AdministratorMapper administratorMapper) {
        this.administratorMapper = administratorMapper;
    }
    private boolean isNotExist(String email){
        return administratorMapper.checkExist(email) <= 0;
    }
    @Override
    public boolean checkLogIn(String email, String password) {
        if (isNotExist(email)) return false;
        return Objects.equals(password, administratorMapper.selectById(email).getPassword());

    }

    @Override
    public boolean changePassword(String email, String oldPassword, String newPassword) {
        if (isNotExist(email)) {
            if (!checkLogIn(email, oldPassword)) return false;
            else {
                Administrator administrator = administratorMapper.selectById(email);
                administrator.setPassword(newPassword);
                return administratorMapper.updateById(administrator) == 1;
            }
        }
        return false;
    }

    @Override
    public boolean register(Administrator administrator) {
        if (isNotExist(administrator.getEmail()))
            return administratorMapper.insert(administrator) == 1;
        return false;
    }


}
